<?php
require_once('../libraryfiles/config.php');
$content = 'selectAccountType.php';
$include = '';
require_once TMP_USER_PATH;
$error = '';
$pharma_id = $_SESSION['acc_id'];
$user_type = $_SESSION['usertype'];
$user_type_new = 2;
$email = $_SESSION['username'];

$account_type = isset($_POST['accounttype']) ? $_POST['accounttype'] : '';
if (empty($account_type)) {
    $error = 'PLSSELECTTHEACCOUNT';
} else {
    /* send email to request user */
    $get_config_user = select(Tblrefcode::tblname, array(Tblrefcode::value), array(Tblrefcode::name => 'mail_user_login', Tblrefcode::type => 'global_setting'));
    $row_config_user = mysql_fetch_assoc($get_config_user);
    $admin_emil = $row_config_user[Tblrefcode::value];

    $ch_user_upgrade = select(Tblrefcode::tblname, '', array(Tblrefcode::oject_id => $pharma_id, Tblrefcode::type => 'user_upgrade'));
    $row_user_upgrade = mysql_fetch_assoc($ch_user_upgrade);
    if (empty($row_user_upgrade)) {
        $data_request = array(
            Tblrefcode::oject_id => $pharma_id,
            Tblrefcode::type => 'user_upgrade',
            Tblrefcode::name => 'user_request_upgrade',
            Tblrefcode::value => $account_type,
        );
        $request = insert(Tblrefcode::tblname, $data_request);
        if ($request == TRUE) {
            $set_user_status = update(Tbluser::Tbluser, array(Tbluser::activated_status => 0), array(Tbluser::id => $pharma_id));
        }

        include_once S_ROOT . '/libraryfiles/SendEmail.php';
        $transport = new SendEmail ();
        $name = '';
        $header = $admin_emil;
        $subjects = "Psarinternet  Request upgrade account";
        $base = "http://" . $_SERVER ["SERVER_NAME"];
        $BodyHeader = "REQUEST UPDATE YOUR ACCOUNT!";
        $BodyMessage = 'Your account is pending, please wait your activation by psarinternet administrator!';
        $transport2 = $transport->SendMail($name, $email, $base, $header, $subjects, $BodyHeader, $BodyMessage);
        /* end send email to request user */

        /* send email to admin */
        $name1 = '';
        $header1 = $admin_emil;
        $subjects1 = "Psarinternet User Request upgrade account";
        $BodyHeader1 = "USER REQUEST!";
        $BodyMessage1 = 'You have a user request to upgrade account, please login to approve that user.';
        $transport1 = $transport->SendMail($name1, $admin_emil, $base, $header1, $subjects1, $BodyHeader1, $BodyMessage1);
        /* end send email to admin */
        if ($transport2 == TRUE && $transport1 == TRUE) {
            echo '<script type="text/javascript">window.location = "'. base_url .'?success=ACCOUNT_UPGRADE_PEDING"</script>';
            //header("Location:" . base_url . '?success=ACCOUNT_UPGRADE_PEDING');
            exit();
        }
    } else {
        echo '<script type="text/javascript">window.location = "'. base_url .'?error=ACCOUNT_UPGRADE_EXIST"</script>';
        //header("Location:" . base_url . '?error=ACCOUNT_UPGRADE_EXIST');
        exit();
    }
}

if (!empty($confirm)) {
    $SET = array(
        Tbluser::account_type_id => $account_type,
        Tbluser::user_type_id => $user_type_new
    );
    $sql_u = update(Tbluser::Tbluser, $SET, array(Tbluser::id => $pharma_id));

    $_SESSION['acc_type'] = $account_type;
    $_SESSION['usertype'] = $user_type_new;
    //header("Location:".W_ROOT.'/seller/');
} ?>
<script type="text/javascript">
    function checkAccountSelect() {
        if (document.forms['frmaccount']['accounttype'][0].checked == false && document.forms['frmaccount']['accounttype'][1].checked == false &&
                document.forms['frmaccount']['accounttype'][2].checked == false && document.forms['frmaccount']['accounttype'][3].checked == false
                ) {
            alert("Please select your Account type!");
            return false;
        }
    }
</script>
<!-- error message -->
<?php if (!empty($_GET['error'])) { ?>
    <div class="row-fluid">
        <div class="span12">
            <?php
            $CURRENT_URL = W_ROOT;
            if (isset($_GET ['error'])) {
                $message = $_GET ['error'];
                ?>
                <div class="alert alert-block alert-error fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <h3 class="alert-heading">
                        <!--Oh snap! You got an error!-->
                        <?php echo $lang['SNAB_ERROO']; ?></h3>
                    <p style="font-size: 16px;">
                    <blink><?php echo @$lang[$message]; ?></blink>
                    </p>
                </div>
                <?php
            }
            ?> 
        </div>
    </div>
<?php } ?>
<!-- end error message -->
<div class="breadcrumb">
    <a class="lever_1" style="z-index:100" href="<?php echo base_url; ?>"><?php echo $lang['HOME']; ?></a>
    <a class="lever_2" style="z-index:99" href="<?php echo base_url; ?>account"><?php echo $lang['SELL_DASH']; ?></a>
    <a class="lever_3" style="z-index:98" href="<?php echo base_url; ?>account/edit_seller.php"><?php echo $lang['USER_UPGRADE_ACCOUNT']; ?></a>
</div>
<div class="row-fluid product-info">
    <div class="span3">
        <?php include(S_ROOT . '/templates/sidebar-user.php'); ?>
        <?php include(S_ROOT . '/templates/sidebar-left.php'); ?>
        
    </div>
    <div class="span9">
        <div class="box laster custom">
            <div class="box-heading"><h2><?php echo $lang['USER_UPGRADE_ACCOUNT']; ?></h2></div>
            <div class="box-content">
                <div class="product-grid" style="padding:10px;">
                    <?php
                    if ($account_type == 0) {
                        ?>
                        <form action="" method="post" name="frmaccount" onsubmit="return checkAccountSelect();">
                            <div id="selectAccountType">                              

                                <?php
                                if ($error != "") {
                                    ?>
                                    <p align="center"><strong><?php echo $lang[$error]; ?></strong></p>
                                    <?php
                                }
                                ?>                                
                                <?php
                                $field = array(
                                    Tblusertype::id,
                                    Tblusertype::name,
                                    Tblusertype::description,
                                    Tblusertype::account_price
										);
                                $result = select(Tblusertype::TABLE, $field, array(Tblusertype::status => 1), $order = 'id DESC');
                                if (dbNumRows($result)) {
                                    $i = 1;
                                    while ($row = mysql_fetch_assoc($result)) {
                                        if ($i % 2 == 1) {
                                            echo '<div class="row-fluid">';
                                        }
                                        ?>
                                        <div class="span6 box" style="padding:10px;min-height: 200px">
                                            <label><div class="accountType_left">
                                                    <div class="left_text">
                                                        <input type="radio" name="accounttype" value="<?php echo $row[Tblusertype::id]; ?>" <?php echo($row[Tblusertype::id] == $_SESSION['acc_type']) ? ' checked="checked"' : '' ?> /> 
                                                        <?php echo $row[Tblusertype::name] ?> <span class="coloRed">( <?php
                                                        if ($row[Tblusertype::account_price] == 0) {
                                                            echo 'Free';
                                                        } else {
                                                            ?>
                                                                $<?php
                                                                echo $row[Tblusertype::account_price];
                                                            }
                                                            ?>
                                                            )</span>
                                                    </div>
                                                </div>
                                                <div class="accountType_right">
                                                    <strong>Description:</strong><br />
                                                    <?php echo $row[Tblusertype::description]; ?>
                                                </div></label>
                                        </div>
                                        <?php
                                        if ($i % 2 == 0) {
                                            echo "</div>";
                                        }
                                        $i++;
                                    }// end while
                                    if ($i % 2 != 1)
                                        echo "</div>";
                                } else {
                                    
                                }// end if
                                ?>                                
                                <table align="center">
                                    <tr>
                                        <td align="center"><input type="submit" value="Submit" class="button" name="btnsubmit"></td>
                                    </tr>
                                </table>    
                            </div>
                        </form>
                        <?php
                    } else {
                        ?>
                        <div style="text-align: center;">Your Account had update, you can post your Product rightnow, <a href="<?php echo base_url; ?>account/product/?view=add">click here to post</a></div>
                        <?php
                    }
                    ?>
                </div>
            </div>
        </div>       
    </div><!--<div class="box">-->
    
</div> <!--<div class="row-fluid">-->